//@PCSTART@//
package com.bluetane.pc;
//@PCEND@//
/*//@ANDROIDSTART@//
 package com.bluetane.android;
 //@ANDROIDEND@//*/

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/*//@ANDROIDSTART@//
import com.bluetane.android.BluetaneEvent.BluetaneEventType;
import android.bluetooth.BluetoothSocket;
 //@ANDROIDEND@//*/
//@PCSTART@//
import com.bluetane.pc.BluetaneEvent.BluetaneEventType;
import javax.microedition.io.StreamConnection;
//@PCEND@//

public class BtConnection
/*//@ANDROIDSTART@//
 extends Thread 
//@ANDROIDEND@//*/
{
	/*//@ANDROIDSTART@//
	private BluetoothSocket socket;
    //@ANDROIDEND@//*/
    //@PCSTART@//
    BtLocalDevice localDevice;
	StreamConnection port;
    //@PCEND@//

        private final InputStream input;
	private final OutputStream output;
    private BluetaneEventSource eventSource;

	public BtConnection(
	/*//@ANDROIDSTART@//
			BluetoothSocket startSocket, BluetaneEventSource startEventSource)
    //@ANDROIDEND@//*/
    //@PCSTART@//
		BtLocalDevice local, StreamConnection connectionPort, BluetaneEventSource startEventSource)
    //@PCEND@//
	{
        //@PCSTART@//
        localDevice=local;
        port=connectionPort;
        //@PCEND@//
        /*//@ANDROIDSTART@//
        socket = startSocket;
        //@ANDROIDEND@//*/
        eventSource = startEventSource;
        InputStream tempInput = null;
        OutputStream tempOutput = null;
        try
        {
            //@PCSTART@//
            tempInput=port.openInputStream();
            tempOutput=port.openOutputStream();
            //@PCEND@//
            /*//@ANDROIDSTART@//
            tempInput = socket.getInputStream();
            tempOutput = socket.getOutputStream();
            //@ANDROIDEND@//*/
         }
        catch (IOException e)
        {

        }
        input = tempInput;
        output = tempOutput;
    }

    //public Object recieve(){
    //    return new Object();//Dummy code
    //}
    //
    //public void close(){
	//
    //}

        /**
         * Thead run on BtConnection makes this new thread wait for SendableObjects
         * It will keep trying to recieve new sendable objects by checking the InputStream
         */
	public void run()
	{
		SendableObject recieved;
		int length;
		byte[] lengthBytes = new byte[4];
		byte[] buffer;
		int lengthRead;
		String sendType;
		// Read from the input until exception
		while (true)
		{
			try
			{
				// Read sendType
				lengthRead = readInput(lengthBytes);
				length = IntUtils.bytesToInt(lengthBytes);
				buffer = new byte[length];
				lengthRead = readInput(buffer);
				sendType = new String(buffer);
				
				// Read sendableObject
				lengthRead = readInput(lengthBytes);
				length = IntUtils.bytesToInt(lengthBytes);
				buffer = new byte[length];
				lengthRead = readInput(buffer);
				recieved = new SendableObject(sendType, buffer);
				eventSource.fireEvent(new BluetaneEvent(this, BluetaneEventType.SendableRecieved, recieved));
			}
			catch (IOException e)
			{
				break;
			}
		}
	}


        /**
         * Reads from intput stream into byte[] b
         *
         * @param b
         * @return number of bytes read
         * @throws java.io.IOException
         */
        private int readInput(byte[] b) throws java.io.IOException
        {
            int i;
            i= input.read(b);
            System.out.print(b);
            System.out.print("...");
            return i;
//            return input.read(b);
        }
	private void write(byte[] bytes)
	{
		try
		{
			output.write(bytes);
		}
		catch (IOException e)
		{
			
		}
	}
		
	public void send(SendableObject sendObject)
	{
		int length;
		byte[] lengthBytes = new byte[4];
		byte[] typeBytes = sendObject.getType().getBytes();
		byte[] objectBytes = sendObject.getBytes();
		
		// Write the object type
		length = typeBytes.length;
		lengthBytes = IntUtils.intToBytes(length);
		this.write(lengthBytes);
		this.write(typeBytes);
		
		//Write the object
		length = objectBytes.length;
		lengthBytes = IntUtils.intToBytes(length);
		this.write(lengthBytes);
		this.write(objectBytes);
	}
		
}
